package com.ytclub.subject.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.ytclub.subject.application.convert.SubjectAnswerDTOConvert;
import com.ytclub.subject.application.convert.SubjectInfoDTOConvert;
import com.ytclub.subject.application.dto.SubjectInfoDTO;
import com.ytclub.subject.common.entity.PageResult;
import com.ytclub.subject.common.entity.Result;
import com.ytclub.subject.domain.entity.SubjectAnswerBO;
import com.ytclub.subject.domain.entity.SubjectInfoBO;
import com.ytclub.subject.domain.service.SubjectInfoDoaminService;
import com.ytclub.subject.infra.basic.entity.SubjectInfoEs;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 刷题Controller
 *
 * @author: HuangQi
 * @date: Created in 23:07 2023/12/13
 * @description: 刷题模块控制层
 */
@RestController
@Slf4j
@RequestMapping("/subject")
public class SubjectController {

    @Autowired
    private SubjectInfoDoaminService subjectInfoDoaminService;

    /**
     * 新增题目
     *
     * @param subjectInfoDTO 新增分类的信息
     * @return Result<Boolean>
     */
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectController.add.dto:{}", JSON.toJSONString(subjectInfoDTO));
            }
            // 参数校验
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectName(), "题目名称不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectDifficult(), "题目难度不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectType(), "题目类型不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectScore(), "题目分数不能为空");
            Preconditions.checkNotNull(CollectionUtils.isEmpty(subjectInfoDTO.getCategoryIds())
                    , "分类id不能为空");
            Preconditions.checkNotNull(CollectionUtils.isEmpty(subjectInfoDTO.getLabelIds())
                    , "分类id不能为空");

            // 参数转换
            SubjectInfoBO subjectInfoBO =
                    SubjectInfoDTOConvert.INSTANCE.convertDTOToInfoBO(subjectInfoDTO);
            List<SubjectAnswerBO> subjectAnswerBOS =
                    SubjectAnswerDTOConvert.INSTANCE.convertListDTOToAnswerBO(subjectInfoDTO.getOptionList());
            subjectInfoBO.setOptionList(subjectAnswerBOS);
            // 调用领域层
            subjectInfoDoaminService.add(subjectInfoBO);

            // 返回结果
            return Result.ok(true);
        } catch (Exception e) {
            // 异常处理
            log.error("SubjectCategoryController.add.error:{}", e.getMessage(), e);
            return Result.fail("新增分类失败");
        }
    }


    /**
     * 查询符合题目列表
     *
     * @param subjectInfoDTO 查询题目的条件
     * @return Result<PageResult < SubjectInfoDTO>> 符合条件的题目列表
     */
    @PostMapping("/getSubjectPage")
    public Result<PageResult<SubjectInfoDTO>> getSubjecPage(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectController.getSubjecPage.dto:{}", JSON.toJSONString(subjectInfoDTO));
            }
            // 参数校验
            Preconditions.checkNotNull(subjectInfoDTO.getCategoryId(), "分类id不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getLabelId(), "标签id不能为空");

            // 参数转换
            SubjectInfoBO subjectInfoBO =
                    SubjectInfoDTOConvert.INSTANCE.convertDTOToInfoBO(subjectInfoDTO);

            // 调用领域层
            PageResult<SubjectInfoBO> infoDTOPageResult = subjectInfoDoaminService.getSubjectPage(subjectInfoBO);

            // 返回结果
            return Result.ok(infoDTOPageResult);
        } catch (Exception e) {
            // 异常处理
            log.error("SubjectCategoryController.getSubjecPage.error:{}", e.getMessage(), e);
            return Result.fail("查询符合题目列表失败");
        }
    }

    /**
     * 查询题目详情
     *
     * @param subjectInfoDTO 该题目的查询条件
     * @return 题目详情
     */
    @PostMapping("/querySubjectInfo")
    public Result<SubjectInfoDTO> querySubjectInfo(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectController.querySubjectInfo.dto:{}", JSON.toJSONString(subjectInfoDTO));
            }
            // 参数校验
            Preconditions.checkNotNull(subjectInfoDTO.getId(), "题目id不能为空");

            // 参数转换
            SubjectInfoBO subjectInfoBO =
                    SubjectInfoDTOConvert.INSTANCE.convertDTOToInfoBO(subjectInfoDTO);

            // 调用领域层
            SubjectInfoBO resultBo = subjectInfoDoaminService.querySubjectInfo(subjectInfoBO);
            SubjectInfoDTO subjectInfoDTOResult = SubjectInfoDTOConvert.INSTANCE.convertBOToDTO(resultBo);
            // 返回结果
            return Result.ok(subjectInfoDTOResult);
        } catch (Exception e) {
            // 异常处理
            log.error("SubjectCategoryController.querySubjectInfo.error:{}", e.getMessage(), e);
            return Result.fail("查询题目详情失败");
        }
    }

    /**
     * 全文检索
     *
     * @param subjectInfoDTO 检索条件
     * @return 检索结果
     */
    @PostMapping("/getSubjectPageBySearch")
    public Result<PageResult<SubjectInfoEs>> getSubjectPageBySearch(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectController.getSubjectPageBySearch.dto:{}", JSON.toJSONString(subjectInfoDTO));
            }
            // 参数校验
            Preconditions.checkArgument(StringUtils.isNotBlank(subjectInfoDTO.getKeyWord()), "关键字不能为空");

            // 参数转换
            SubjectInfoBO subjectInfoBO =
                    SubjectInfoDTOConvert.INSTANCE.convertDTOToInfoBO(subjectInfoDTO);
            subjectInfoBO.setPageNo(subjectInfoDTO.getPageNo());
            subjectInfoBO.setPageSize(subjectInfoDTO.getPageSize());

            // 调用领域层
            PageResult<SubjectInfoEs> boPageResult = subjectInfoDoaminService.getSubjectPageBySearch(subjectInfoBO);
            // 返回结果
            return Result.ok(boPageResult);
        } catch (Exception e) {
            // 异常处理
            log.error("SubjectCategoryController.getSubjectPageBySearch.error:{}", e.getMessage(), e);
            return Result.fail("全文检索失败");
        }
    }


    /**
     * 获取题目贡献榜
     *
     * @return 贡献榜单
     */
    @PostMapping("/getContributeList")
    public Result<List<SubjectInfoDTO>> getContributeList() {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectController.getContributeList");
            }

            // 调用领域层
            List<SubjectInfoBO> boList = subjectInfoDoaminService.getContributeList();
            List<SubjectInfoDTO> dtoList = SubjectInfoDTOConvert.INSTANCE.convertListBOToDTO(boList);

            // 返回结果
            return Result.ok(dtoList);
        } catch (Exception e) {
            // 异常处理
            log.error("SubjectCategoryController.getContributeList.error:{}", e.getMessage(), e);
            return Result.fail("全文检索失败");
        }
    }
}
